package com.dlwx.parttimetalent.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.baidu.location.BDLocation;
import com.dlwx.parttimetalent.baidu.BaiduListener;
import com.dlwx.parttimetalent.baidu.LocationService;
import com.dlwx.parttimetalent.base.MyApplication;
import com.dlwx.parttimetalent.bean.BaseBean;
import com.dlwx.parttimetalent.comm.Constant;
import com.dlwx.parttimetalent.comm.Http;
import com.dlwx.parttimetalent.inter.LocationInter;
import com.dlwx.parttimetalent.utils.DealResult;
import com.dlwx.parttimetalent.utils.NetUtils;
import com.dlwx.parttimetalent.utils.OkgoUtil;
import com.dlwx.parttimetalent.utils.SPUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

/**
 * Created by Administrator on 2018/1/2.
 */

public class MyService extends Service {
    private BaiduListener mListener;
    private LocationService locationService;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(locationService==null){
            locationService = ((MyApplication) getApplication()).locationService;
        }
        if(mListener==null){
            mListener=new BaiduListener(this, new LocationInter() {
                @Override
                public void getAddressInfo(BDLocation location) {
                    Intent intent = new Intent("com.dlwx.location");
                    intent.putExtra("location",location);
                    sendBroadcast(intent);
                    SPUtils.put(getApplicationContext(),Constant.LOCATION_LON,location.getLongitude()+"");
                    SPUtils.put(getApplicationContext(),Constant.LOCATION_LAT,location.getLatitude()+"");
                    SPUtils.put(getApplicationContext(),Constant.ADDRESS,location.getAddress().address+"");
//                LogUtils.i("------>"+location.getCity());
                    String city=location.getCity();
                    if(city.endsWith("市")){
                        city= city.substring(0,city.length()-1);
                    }
                    SPUtils.put(getApplicationContext(),Constant.CITY,city);
                    if(NetUtils.isConnected(getApplicationContext())){
                        updateLocation(location);
                    }
                }
            });
            //获取locationservice实例，建议应用中只初始化1个location实例，然后使用，可以参考其他示例的activity，都是通过此种方式获取locationservice实例的
            locationService.registerListener(mListener);
        }
        //注册监听
        locationService.setLocationOption(locationService.getDefaultLocationClientOption());
        locationService.start();// 定位SDK
        return super.onStartCommand(intent,flags,startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locationService.unregisterListener(mListener); //注销掉监听
        locationService.stop(); //停止定位服务
    }


    private void updateLocation(BDLocation location) {
        String city=location.getCity();
        if(city.endsWith("市")){
            city= city.substring(0,city.length()-1);
        }
        OkgoUtil.post(Http.MyPosition, this)
                .params("long",location.getLongitude())
                .params("lat",location.getLatitude())
                .params("city",city)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        BaseBean parentBean = DealResult.getInstance().dealDatas(getApplicationContext(), response, new TypeToken<BaseBean>() {}.getType());
                        if (parentBean == null) {
                            return;
                        }
                        if(parentBean.getCode()== Constant.SUCESS){
                        }
//                        ToastUtils.toastForShort(BaseActivity.this, parentBean.getResult());
                    }
                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
//                        ToastUtils.toastForShort(getApplicationContext(),getString(R.string.bad_net));
                    }
                });
    }
}
